package lt.mii.optimum.io;

import java.io.File;
import java.io.Serializable;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

/**
 * Class XmlReadData Enables to read XML files (XLS files saved as XML)
 * 
 * @author dstrukov
 * 
 */
public class XmlReadData implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6059979576613556296L;

	private int row_count[], column_count[], sheet_count;
	private String name[];

	private NodeList[] Data;



	/**
	 * Contructor<br>
	 * Does actual reading.
	 * 
	 * @param Path
	 *            Full Path to XML file.
	 */
	public XmlReadData(String Path) {

		try {
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.parse(new File(Path));
			doc.getDocumentElement().normalize();

			NodeList worksheets = doc.getElementsByTagName("Worksheet");
			sheet_count = worksheets.getLength();
			if (sheet_count <= 0) {
				worksheets = doc.getElementsByTagName("ss:Worksheet");
				sheet_count = worksheets.getLength();
			}

			Data = new NodeList[sheet_count];
			row_count = new int[sheet_count];
			column_count = new int[sheet_count];
			name = new String[sheet_count];

			for (int i = 0; i < sheet_count; i++) {
				Node worksheet = worksheets.item(i);
				Element ws = (Element) worksheet;
				NodeList columns = ws.getElementsByTagName("Cell");
				NodeList rows = ws.getElementsByTagName("Row");
				row_count[i] = rows.getLength(); // eiluciu kiekis
				column_count[i] = columns.getLength() / row_count[i] + 1;
				Data[i] = rows;
				name[i] = ws.getAttribute("ss:Name");
			}

		} catch (SAXParseException err) {
			System.out.println("** Parsing error" + ", line "
					+ err.getLineNumber() + ", uri " + err.getSystemId());
			System.out.println(" " + err.getMessage());
		}

		catch (SAXException e) {
			Exception x = e.getException();
			(x == null ? e : x).printStackTrace();
		}

		catch (Throwable t) {
			t.printStackTrace();
		}
	}



	/*
	 * XmlReadData(int SheetNr, String Path) { //this.Path =Path; try {
	 * DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
	 * .newInstance(); DocumentBuilder docBuilder =
	 * docBuilderFactory.newDocumentBuilder(); Document doc =
	 * docBuilder.parse(new File(Path)); doc.getDocumentElement().normalize();
	 * 
	 * NodeList worksheets = doc.getElementsByTagName("Worksheet"); sheet_count =
	 * worksheets.getLength(); Node worksheet = worksheets.item(SheetNr);
	 * Element ws = (Element) worksheet; NodeList columns =
	 * ws.getElementsByTagName("Cell"); NodeList rows =
	 * ws.getElementsByTagName("Row");
	 * 
	 * row_count = rows.getLength(); // eiluciu kiekis column_count =
	 * columns.getLength() / row_count + 1; // stulpeliu // kiekis Data = rows;
	 * name = ws.getAttribute("ss:Name"); } catch (SAXParseException err) {
	 * System.out.println("** Parsing error" + ", line " + err.getLineNumber() + ",
	 * uri " + err.getSystemId()); System.out.println(" " + err.getMessage()); }
	 * 
	 * catch (SAXException e) { Exception x = e.getException(); ((x == null) ? e :
	 * x).printStackTrace(); }
	 * 
	 * catch (Throwable t) { t.printStackTrace(); } }
	 */

	public int getColumn_count(int SheetNr) {
		return column_count[SheetNr];
	}



	public NodeList getData(int SheetNr) {
		return Data[SheetNr];
	}



	public int getRow_count(int SheetNr) {
		return row_count[SheetNr];
	}



	public int getSheet_count() {

		return sheet_count;
	}



	public String getName(int SheetNr) {
		return name[SheetNr];
	}

}
